package com.duowei.diandan.bean.food;

import com.duowei.diandan.db.entry.Tcsd;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 套餐
 */
public class Meal implements FoodImpl {

    /** 主项 */
    private Tcsd mMainTcsd;

    /** 分类子项 */
    private List<MealChildGroup> mMealChildGroupList;

    public Meal(Tcsd mainTcsd, List<MealChildGroup> mealChildGroupList) {
        mMainTcsd = mainTcsd;
        mMealChildGroupList = mealChildGroupList;
    }

    public Tcsd getMainTcsd() {
        return mMainTcsd;
    }

    public MealChild getMealChild() {
        List<MealChildChecked> mealChildCheckedList = new ArrayList<>();
        List<MealChildGroupAndMealChildChecked> mealChildGroupAndMealChildCheckedList = new ArrayList<>();
        MealChild mealChild = new MealChild(mealChildCheckedList, mealChildGroupAndMealChildCheckedList);

        for (int i = 0; i < mMealChildGroupList.size(); i++) {
            MealChildGroup mcg = mMealChildGroupList.get(i);

            if (mcg.getChildList().size() == 1) {
                mealChildCheckedList.add(new MealChildChecked(
                        mcg.getCateList(), mcg.getChildList().get(0), true));

            } else if (mcg.getChildList().size() > 1) {
                MealChildChecked mcc = new MealChildChecked(mcg.getCateList(), null, false);
                mealChildCheckedList.add(mcc);

                MealChildGroupAndMealChildChecked mcgamcc = new MealChildGroupAndMealChildChecked(mcg, mcc);
                mealChildGroupAndMealChildCheckedList.add(mcgamcc);
            }
        }

        return mealChild;
    }

    public List<Tcsd> getCheckedList() {
        List<Tcsd> list = new ArrayList<>();

        for (MealChildGroup mcg : mMealChildGroupList) {
            if (mcg.getChildList() != null) {
                List<Tcsd> tcsdList = mcg.getChildList();

                if (tcsdList.size() == 1) {
                    list.add(tcsdList.get(0));

                } else if (tcsdList.size() > 1) {
                    for (Tcsd t : tcsdList) {
                        if ("1".equals(t.getSfxz())) {
                            list.add(t);
                            break;
                        }
                    }
                }
            }
        }

        return list;
    }

    public List<Tcsd> getAllTcsdList() {
        List<Tcsd> tcsdAllList = new ArrayList<>();

        for (MealChildGroup mcg : mMealChildGroupList) {
            List<Tcsd> tcsdList = mcg.getChildList();
            if (tcsdList != null) {
                tcsdAllList.addAll(tcsdList);
            }
        }

        return tcsdAllList;
    }

    public void clearCheckedTaste() {
        List<Tcsd> tcsdList = getAllTcsdList();
        for (Tcsd t : tcsdList) {
            t.cleanTasteChecked();
        }
    }

    public static List<Tcsd> getHasTateTcsdList(List<Tcsd> tcsdList) {
        List<Tcsd> tasteTcsdList = new ArrayList<>();

        for (Tcsd t : tcsdList) {
            List<Taste> tasteList = t.getTasteList();
            if (tasteList != null && tasteList.size() > 0) {
                tasteTcsdList.add(t);
            }
        }

        return tasteTcsdList;
    }

    public boolean isAllChildChecked() {
        if (getCheckedList().size() == mMealChildGroupList.size()) {
            return true;
        } else {
            return false;
        }
    }

    public int getChildTotalSize() {
        return mMealChildGroupList.size();
    }

    /**
     * @return 获取只有一项选择的TCSD列表
     */
    public List<Tcsd> getOnlyItemList() {
        List<Tcsd> list = new ArrayList<>();

        for (int i = 0; i < mMealChildGroupList.size(); i++) {
            MealChildGroup mealChildGroup = mMealChildGroupList.get(i);

            if (mealChildGroup != null && mealChildGroup.getChildList() != null) {
                List<Tcsd> tcsdList = mealChildGroup.getChildList();
                for (int j = 0; j < tcsdList.size(); j++) {
                    if (tcsdList.size() == 1) {
                        list.add(tcsdList.get(0));
                    }
                }
            }
        }

        return list;
    }

    /**
     * @return 获取多项项选择的MealChildGroup列表
     */
    public List<MealChildGroup> getMultiItemList() {
        List<MealChildGroup> list = new ArrayList<>();

        for (int i = 0; i < mMealChildGroupList.size(); i++) {
            MealChildGroup mcg = mMealChildGroupList.get(i);

            if (mcg.getChildList() != null && mcg.getChildList().size() > 1) {
                list.add(mcg);
            }
        }

        return list;
    }

    public List<MealChildGroup> getMealChildGroupList() {
        return mMealChildGroupList;
    }

    @Override
    public String getImgNo() {
        if (mMainTcsd != null) {
            return mMainTcsd.getJyxmszTm();
        } else {
            return null;
        }
    }

    @Override
    public String getName() {
        if (mMainTcsd != null) {
            return mMainTcsd.getXmmc1();
        } else {
            return null;
        }
    }

    @Override
    public Float getPrice() {
        return mMainTcsd.getPrice();
    }

    @Override
    public Float getMemberPrice() {
        return mMainTcsd.getMemberPrice();
    }

    @Override
    public Float getXl() {
        if (this.mMainTcsd != null) {
            return this.mMainTcsd.getXl();
        } else {
            return null;
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Meal) {
            Meal meal = (Meal) obj;

            return Objects.equals(this.mMainTcsd, meal.mMainTcsd)
                    && (this.mMealChildGroupList != null && this.mMealChildGroupList.containsAll(meal.mMealChildGroupList))
                    && (meal.mMealChildGroupList != null && meal.mMealChildGroupList.containsAll(this.mMealChildGroupList));

        } else {
            return false;
        }
    }
}
